<template>
  <div>
    <el-card>
      <template #header>
        <el-button
          type="primary"
          size="default"
          icon="el-icon-plus"
          @click.native="opendialog"
          >点击添加</el-button
        >
      </template>
      <el-table v-loading="loading" :data="tradeMarkList.records" border stripe>
        <el-table-column type="index" width="50" label="序号" />
        <el-table-column label="品牌名称" prop="tmName"> </el-table-column>
        <el-table-column label="品牌LOGO">
          <template v-slot="{ row }">
            <img :src="row.logoUrl" width="100" alt="" />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              type="warning"
              size="small"
              icon="el-icon-edit"
              @click="reWrite(row)"
            ></el-button>

            <el-popconfirm
              :style="{ marginLeft: '20px' }"
              title="这是一段内容确定删除吗？"
              @onConfirm="del(row.id)"
            >
              <el-button
                type="danger"
                size="small"
                icon="el-icon-delete"
                slot="reference"
              ></el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page.sync="table.page"
        :page-sizes="[3, 5, 7]"
        :page-size.sync="table.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tradeMarkList.total"
        background
      >
      </el-pagination>

      <el-dialog title="添加品牌" :visible.sync="dialogFormVisible" width="60%">
        <el-form
          label-width="100px"
          :inline="false"
          size="normal"
          :rules="rules"
          :model="addFileInfo"
          ref="FileFrom"
        >
          <el-form-item label="品牌名称" prop="tmName">
            <el-input v-model="addFileInfo.tmName"></el-input>
          </el-form-item>
          <el-form-item label="品牌LOGO" size="normal" prop="logoUrl">
            <el-upload
              class="avatar-uploader"
              :action="`${baseURl}/admin/product/fileUpload`"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img
                v-if="addFileInfo.logoUrl"
                :src="addFileInfo.logoUrl"
                class="avatar"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-form>

        <span slot="footer">
          <el-button @click="dialogFormVisible = false">关闭</el-button>
          <el-button type="primary" @click="addTrademark(addFileInfo)"
            >确定</el-button
          >
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {
  reqtrademarkList,
  reqaddTrademark,
  updataTradmarkl,
  deltrademarkItem,
} from "@/api/trademake";
export default {
  name: "Trademark",
  data() {
    return {
      table: {
        page: 1,
        limit: 3,
      },
      tradeMarkList: {
        records: [],
        total: 0,
      },
      // 弹出框
      dialogFormVisible: false,
      // 上传数据
      imageUrl: "",
      loading: false,
      //创建环境变量前缀
      baseURl: process.env.VUE_APP_BASE_API,
      addFileInfo: {
        logoUrl: "",
        tmName: "",
        id: "",
      },
      // 表单验证数据
      rules: {
        tmName: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        logoUrl: [{ required: true, message: "图片是必须的", trigger: "blur" }],
      },
    };
  },
  methods: {
    async gettrademarkList() {
      this.loading = true;
      let re = await reqtrademarkList(this.table.page, this.table.limit);
      console.log(re);
      this.tradeMarkList.records = re.records;
      this.tradeMarkList.total = re.total;
      this.loading = false;
    },
    handleAvatarSuccess(res, file) {
      this.addFileInfo.logoUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    // 点击展开dialog
    opendialog() {
      // 点击添加时需要先删除原始文本
      this.addFileInfo = {
        logoUrl: "",
        tmName: "",
        id: "",
      };
      this.dialogFormVisible = true;
    },
    // 添加品牌信息
    async addTrademark(FileFrom) {
      // 表单的整体验证
      // this.$refs("FileFrom");
      this.$refs["FileFrom"].validate(async (valid) => {
        if (valid) {
          //判断是否有id,如果有id就发送修改请求,没有就发送添加请求
          if (this.addFileInfo.id) {
            await updataTradmarkl(this.addFileInfo);
            this.$message({
              message: "修改成功",
              type: "success",
            });
          } else {
            await reqaddTrademark(this.addFileInfo);
            this.$message({
              message: "添加成功",
              type: "success",
            });
          }
          this.gettrademarkList();
          this.dialogFormVisible = false;
        } else {
          return;
        }
      });
    },
    // 修改数据,进行数据回填
    reWrite(row) {
      this.addFileInfo = row;
      this.dialogFormVisible = true;
    },
    //删除单个品牌
    async del(index) {
      await deltrademarkItem(index);
    },
  },
  mounted() {
    this.gettrademarkList();
  },
  watch: {
    table: {
      deep: true,
      handler() {
        this.gettrademarkList();
      },
    },
  },
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
